﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cole.Entities;
using Cole.Service;

namespace Cole.GUI
{
    public partial class frmDocentes : Form
    {
        private IDocenteService docenteService;
        
        private bool IsNew = true;
        public frmDocentes()
        {
            InitializeComponent();
            if (docenteService == null)
                docenteService = new DocenteService();
        

           
        }

        public void NuevoDocente()
        {
            lblTitulo.Text = "Nuevo Docente";
            docenteBindingSource.AddNew();
           
        }

        public void EditarDocente(Int32 id)
        {
            IsNew = false;
            lblTitulo.Text = "Editar Docente";
            docenteBindingSource.DataSource = docenteService.GetFromDocenteById(id);
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                var docente = docenteBindingSource.Current as Docente;
                if (IsNew)
                {
                    if (docente!=null)
                    {
                        if (nombresTextBox.Text != "")
                        {
                            if (apellidosTextBox.Text != "")
                            {
                                if (especialidadTextBox.Text != "")
                                {
                                    docenteService.AddDocente(docente);
                                    MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                                    this.Close();
                                }
                                lblEspecialidad.Text = "*";
                            }
                            lblApellidos.Text = "*";
                        }
                        else
                        {
                            lblNombre.Text = "*";
                        }
                        
                    }
                    else
                    {
                        MessageBox.Show("No se puedo registrar el Docente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    if (docente!=null)
                    {
                        docenteService.UpdateDocente(docente);
                    }
                }
                

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Mensaje",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void nombresTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = guion + amberson + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }

        private void apellidosTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = guion + amberson + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }

        private void especialidadTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = guion + amberson + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }
    }
}
